/*
 * @Author: wangjun haodreams@163.com
 * @Date: 2024-07-24 16:09:12
 * @LastEditors: wangjun haodreams@163.com
 * @LastEditTime: 2024-07-24 16:28:18
 * @FilePath: \godriver\opdb\example\node\main.go
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
package main

import (
	"log"

	"gitee.com/haodreams/godriver/opdb"
	"gitee.com/haodreams/godriver/opdb/opsql"
	"gitee.com/haodreams/golib/logs"
	"gitee.com/haodreams/libs/ee"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
)

func main() {
	dataSource := "sis:openplant@10.202.8.195:8200?timeout=60"
	dialector := opsql.Open(dataSource)
	if dialector == nil {
		ee.NewError("不支持的驱动:" + dataSource).Once2(logs.CbErrorf)
		return
	}
	op, err := gorm.Open(dialector, &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true,
		},
	})

	if err != nil {
		log.Fatalln(err)
	}
	db, err := op.DB()
	if err != nil {
		log.Println(err)
		return
	}
	records, err := db.Query("Read NODE", "W3.XR")
	if err != nil {
		log.Println(err)
	}
	defer records.Close()
	var rows []*opdb.Node

	if records.Next() {
		err = op.ScanRows(records, &rows)
		if err != nil {
			log.Println(err)
			return
		}
	} else {
		return
	}

	for _, row := range rows {
		log.Println(row.ID, row.Name, row.Desc)
	}
}
